package com.sa07.ui.components;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.sa07.bean.User;
import com.sa07.config.LoginSession;
import com.sa07.utils.DateUtils;
import com.sa07.utils.StringUtils;

/**
 * 登录信息栏
 * 
 * @author Long.shangbo
 * 
 *         2014-2-17 下午9:44:38
 */
public class JloginInfoBar extends JPanel {

	private static final long serialVersionUID = 6657781844906428285L;
	
	private JLabel loginUserLbl = null;
	
	private JLabel loginUserRole = null;
	
	private JLabel loginDate = null;

	private JLabel executeTime = null;
	
	private int execHours = 0;
	
	private int execMinutes = 0;
	
	private int execSeconds = 0;
	
	private User loginUser = LoginSession.INSTANCE.getUser();
 	
	public JloginInfoBar() {
		setLayout(new FlowLayout(FlowLayout.LEFT));
		setBorder(BorderFactory.createEtchedBorder());
		initComponents();
		showLoginUser();
		showLoginUserType();
		setLoginDate();
		//启动任务定时刷新执行时间
		refreshExecuteDate();
		new Thread(new ExecuteTimeUpdateTask()).start();
	}
	
	/**
	 * 显示登录用户 
	 */
	public void showLoginUser(){
		String userName = loginUser.getUserName();
		this.loginUserLbl.setText(String.format("作业员：%s", StringUtils.isNullOrEmpty(userName) ? "unknow user" : userName));
	}
	
	/**
	 * 显示登录用户类型
	 */
	public void showLoginUserType(){
		String userType = loginUser.getRole().getRoleName();
		this.loginUserRole.setText(String.format("角色：%s", StringUtils.isNullOrEmpty(userType) ? "unknow role" : userType));
	}
	
	/**
	 * 设置登录时间(当前时间)
	 */
	private void setLoginDate(){
		this.loginDate.setText(String.format("登录时间：%s", 
						DateUtils.formatWithDefaultPattern(new Date())));
	}
	
	private void addExecSecond(){
		if(execSeconds >= 60){
			addExecMinute();
			execSeconds = 0;
		}else{
			execSeconds ++;
		}
	}
	
	
	private void addExecMinute() {
		if(execMinutes > 60){
			addExecHour();
			execMinutes = 0;
		}else{
			execMinutes ++;
		}
	}

	private void addExecHour() {
		execHours ++;
	}

	/**
	 * 刷新执行时间
	 */
	private void refreshExecuteDate(){
		executeTime.setText(String.format("运行时间：%s时%s分%s秒", 
				execHours < 10 ? "0" + execHours : execHours, 
				execMinutes < 10 ? "0" + execMinutes : execMinutes, 
				execSeconds < 10 ? "0" + execSeconds : execSeconds));
	}
	
	
	private void initComponents() {
		int loginInfoHeight = 15;

		loginUserLbl = new JLabel();
		int loginUserWidth = 160;
		loginUserLbl.setPreferredSize(new Dimension(loginUserWidth, loginInfoHeight));
		add(loginUserLbl);
		
		loginUserRole = new JLabel();
		int loginUserRoleWidth = 160;
		loginUserRole.setPreferredSize(new Dimension(loginUserRoleWidth, loginInfoHeight));
		add(loginUserRole);

		loginDate = new JLabel();
		int loginDateWidth = 220;
		loginDate.setPreferredSize(new Dimension(loginDateWidth, loginInfoHeight));
		add(loginDate);

		executeTime = new JLabel();
		int executeTimeWidth = 250;
		loginDate.setPreferredSize(new Dimension(executeTimeWidth, loginInfoHeight));
		add(executeTime);
	}
	
	/**
	 * 执行时间更新任务类
	 * 
	 * @author Long.shangbo
	 *
	 * 2014-2-17 下午10:12:24
	 */
	private class ExecuteTimeUpdateTask implements Runnable{

		@Override
		public void run() {
			while(true){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				addExecSecond();
				refreshExecuteDate();
			}
		}
		
	}
}
